<template>
  <view class="search-box">
    <uni-search-bar :focus="true" :radius="100" bgColor="#fcfcfc" cancelButton="false" placeholder="请输入搜索内容"  @input="input" ></uni-search-bar>
    <!-- 搜索结果 -->
    <view class="search-list" v-if="searchList.length !== 0">
      <view @click="goDetail(item.goods_id)" class="search-item" v-for="(item,index) in searchList" :key="item.goods_id">
        <view class="title">{{item.goods_name}}</view>
        <uni-icons type="right" size="16"></uni-icons>
      </view>
    </view>
    
    <!-- 搜索历史 -->
    <view class="search-history" v-else>
      <view class="title">
        <text>搜索历史</text>
        <uni-icons @click="clearHistory" type="trash-filled" size="20"></uni-icons>
      </view>
      <view class="tags">
        <uni-tag @click="goGoodsList(item)" v-for="(item, index) in historys" :text="item" :key="index"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        kw: '',
        timer: null,
        searchList: [],
        historyList: ['test1', 'test2', 'test3']
      }
    },
    
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw'))  || []
    },
    
    computed: {
      historys: function () {
        return [...this.historyList].reverse()
      }
    },
    
    methods: {
      input(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e
          this.goSearch()
        }, 500)
      },
      
      async goSearch() {
        if(!this.kw) {
          this.searchList = []
          return
        }
        const {data:{message}, statusCode} = await uni.$http.get('api/public/v1/goods/qsearch', {
          query: this.kw
        })
        if(statusCode !== 200) return uni.$showMsg()
       this.searchList = message
       this.addHistory()
      },
      
      goDetail(id) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
        })
      },
      
      addHistory() {
        // this.historyList.push(this.kw)
        const set = new Set(this.historyList)
        set.delete(this.kw)
        set.add(this.kw)
        this.historyList = Array.from(set)
        uni.setStorageSync('kw', JSON.stringify(this.historyList)) 
      },
      
      clearHistory() {
        this.historyList = []
        uni.setStorageSync('kw', JSON.stringify(this.historyList)) 
      },
      
      goGoodsList(item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + item
        })
      }
    }
  }
</script>

<style lang="scss">
  .search-box{

  }
  .uni-searchbar{
    background-color: #C50000;
  }
  
  // 搜索结果
  .search-list{
    padding: 0 5px;
    .search-item{
      font-size: 12px;
      height: 40px;
      align-items: center;
      display: flex;
      justify-content: space-between;
      border-bottom: 1px solid #aeaeae;
      .title{
        padding-right: 10px;
        box-sizing: border-box;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
    }
  }
  
  // 搜索历史
  .search-history{
    .title{
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 5px;
      border-bottom: 1px solid #aeaeae;
      overflow: hidden;
      margin-bottom: 5px;
      text{
        font-size: 12px
      }
    }
    .tags{
      overflow: hidden;
      display: flex;
      flex-wrap: wrap;
      padding: 0 5px;
      .uni-tag {
        height: 50px;
        line-height: 25px;
        overflow: hidden;
        margin-right: 5px;
        background-color: #eeeeee;
        border: none;
        color: #000;
      }
    }
  }
</style>
